﻿
#include "stdafx.h"
#include "qgdiobject.h"

 
using namespace micro::gui;


// Constructor. Initializes to the passed
// handle or nullptr.
QRegion::QRegion( HRGN hGdiObject  )
{
	// Setup the data.
	m_hRegion = hGdiObject;
	 
	m_bDestroy =   !m_hRegion;


 
}

// Destructor. Destroys the wrapped object
// if desired.
QRegion::~QRegion()
{ 
	// Destroy the object.
	if (m_bDestroy && m_hRegion)
		Delete();
}

// Destroys the object if it is marked to be destroyed.
void QRegion::Delete()
{

	::DeleteObject( m_hRegion)  ;

}

 

// Attach a handle to the current object.
void QRegion::Attach( HRGN hGdiObject  )
{
	ASSERT( hGdiObject ); // Passed handle must be valid.
	ASSERT( hGdiObject != m_hRegion ); // Can't attach to ourselves.

	// Delete the current handle.
	Delete();

 
	// Setup the new handle.
	m_hRegion = hGdiObject;
	 m_bDestroy   = false;
}

// Detach the wrapped handle.
HRGN QRegion::Detach()
{
	// Get result.
	HRGN hObject = m_hRegion;

	// Clear handle.
	m_hRegion = nullptr;
	m_bDestroy   = false;
 
	// Return result.
	return hObject;
}

// Is the object valid?
bool QRegion::IsValid()
{
	return ( bool )( m_hRegion != nullptr );
}

 

// Create a temporary object.
QRegion QRegion::FromHandle( HRGN hGdiObject )
{
	ASSERT( hGdiObject ); // Must be valid.
	 return    QRegion( hGdiObject  );
	
}

 
// Operator overload. Returns
// the wrapped handle.
QRegion::operator HRGN() const
{
	// Return the handle.
	return m_hRegion;
}